<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.jsdelivr.net/npm/axios@0.12.0/dist/axios.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/lodash@4.13.1/lodash.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="example">
    <p>Original message: "{{message}}"</p>
    <p>Computed reversed message: "{{reversedMessage}}"</p>
</div>
<div id="demo">{{fullName}}</div>

<div id="watch-example">
    <p>
        Ask a yes/no question:
        <input v-model="question"/>
    </p>
    <p>{{answer}}</p>
</div>
<script>
    var watchExampleVM = new Vue({
        el: '#watch-example',
        data: {
            question: '',
            answer: 'I cannot give you an answer until you ask a question!'
        },
        watch: {
            //如果`question`发生改变,这个函数就会运行
            question: function (newQuestion, oldQuestion) {
                this.answer = 'Waiting for you to stop typing...';
                this.debouncedGetAnswer()
            }
        },
        created: function () {
            this.debouncedGetAnswer = _.debounce(this.getAnswer, 500)
        },
        methods: {
            getAnswer: function () {
                if (this.question.indexOf('?') === -1) {
                    this.answer = 'Questions usually contain a question mark. ;-)';
                    return;
                }
                this.answer = 'Thinking...';
                var vm = this;
                axios.get('https://yesno.wtf/api')
                    .then(function (response) {
                        vm.answer = _.capitalize(response.data.answer);
                    })
                    .catch(function (error) {
                        vm.ans = 'Error! Could not reach the API. ' + error;
                    })
            }
        }
    });
    var demo = new Vue({
        el: '#demo',
        data: {
            firstName: 'Foo',
            lastName: 'Bar',
            // fullName: 'Foo Bar'
            fullName: function () {
                return this.firstName + ' ' + this.lastName
            }
        },
        // watch: {
        //     firstName: function (val) {
        //         this.fullName = val + ' ' + this.lastName
        //     },
        //     lastName: function (val) {
        //         this.fullName = this.firstName + ' ' + val
        //     }
        // }
    });
    var vm = new Vue({
        el: '#example',
        data: {
            message: 'hello'
        },
        computed: {
            //这个是有缓存的
            //计算属性的getter
            reversedMessage: function () {
                return this.message.split('').reverse().join('') + Date.now();
            }
        }
    });
</script>

</body>
</html>